package com.oneraku.service.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: hui.ren@huberbuy.com
 * @Date: 2020/1/10 16:42
 */
@Data
public class RequestBean {
    private String requestId;
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private LocalDateTime stamp;
    private String uri;
    private String url;
    private String ip;
    private Long duration = 0L;
    private Object result;
    private Object header;
    private Object parameter;
    private Boolean exception = false;
    private List<String> traceList;

    public RequestBean(HttpServletRequest request) {
        this.requestId = UUID.randomUUID().toString().toUpperCase();
        this.stamp = LocalDateTime.now();
        this.url = request.getRequestURL().toString();
        this.ip = getIpAddress(request);
        this.requestId = UUID.randomUUID().toString().toUpperCase();

        JSONObject headerObj = new JSONObject();
        //设置头部请求参数
        Enumeration<String> headers =  request.getHeaderNames();
        String host = "";
        while (headers.hasMoreElements()) {
            String headerName = headers.nextElement();
            if(!"cookie".equals(headerName) && !"accept-language".equals(headerName)){
                headerObj.put(headerName,request.getHeader(headerName));
            }
        }
        this.header = headerObj;
        //设置get请求参数
        String queryString = request.getQueryString();
        if(!StringUtils.isEmpty(queryString)){
            this.uri =  host+request.getServletPath()+"?"+queryString;
        }else{
            this.uri = host+request.getServletPath();
        }
    }
    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    public void setException(Exception e){
        List<String> traceList = new ArrayList();
        if (e instanceof NullPointerException) {
            traceList.add("空指针异常");
        } else {
            traceList.add(e.getClass().toString() + "  " + e.getMessage());
        }
        traceList.addAll((Collection) Arrays.asList(e.getStackTrace()).stream()
                .map((item) -> item.toString()).filter((item) -> item.startsWith("net.sangon")).collect(Collectors.toList()));
        this.traceList = traceList;
        this.exception = true;
    }
}
